package ljl.alg.wangzheng_camp.round1.bisection;

public class _154_rotate_array_min {
    
    /*
     * 居然蒙对了？不可思议
     * */
    public int findMin(int[] nums) {
        int n = nums.length;
        int l = 0, r = n - 1;
        while (l <= r) {
            int mid = (l + r) >>> 1;
            if (nums[mid] == nums[l] && nums[l] == nums[r]) {
                l++; r--;
            } else if (nums[mid] >= nums[l]) {
                if (nums[mid] > nums[r]) l = mid + 1;
                else return nums[l];
            } else
                r = mid;
        }
        return nums[l - 1];
    }
    
    public int findMin_lf(int[] nums) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = (left + right) >>> 1;
            if (nums[mid] > nums[right]) left = mid + 1;
            else if (nums[mid] < nums[right]) right = mid;
            else right--;
        }
        return nums[left];
    }
    
}
